/**
*┌──────────────────────────────────────────────────────────────┐
*│　描    述：Manager_role                                                    
*│　作    者：NetCoreCMS                                            
*│　版    本：1.0    模板代码自动生成                                                
*│　创建时间：2019-12-25 10:52:46                             
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│　命名空间： NetCoreCMS.Services                                  
*│　类    名： Manager_roleService                                    
*└──────────────────────────────────────────────────────────────┘
*/
using NetCoreCMS.IRepository;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NetCoreCMS.Services
{
    public class Manager_roleService: IManager_roleService
    {
        private readonly IManager_roleRepository _repository;
        private readonly IManager_role_valueRepository _role_value;

        public Manager_roleService(IManager_roleRepository repository, IManager_role_valueRepository role_value)
        {
            _repository = repository;
            _role_value = role_value;
        }

        public int Add(Manager_role model)
        {
            return _repository.Add(model);
        }

        public bool Delete(int id)
        {
            return _repository.DeleteAll(id);
        }

        /// <summary>
        /// 检查是否有权限
        /// </summary>
        public bool Exists(int role_id, string nav_name, string action_type)
        {
            var model = _repository.GetModel(role_id);
            if (model != null)
            {
                if (model.role_type == 1)
                {
                    return true;
                }
                Models.Manager_role_value modelt = model.manager_role_values.Find(p => p.nav_name == nav_name && p.action_type == action_type);
                if (modelt != null)
                {
                    return true;
                }
            }
            return false;
        }

        public bool Exists(int id)
        {
            return _repository.Get(id) != null ? true : false;
        }

        public List<Manager_role> GetList()
        {
            return _repository.GetList().ToList();
        }

        public List<Manager_role> GetList(string conditions)
        {
            return _repository.GetList(conditions).ToList();
        }

        public Manager_role GetModel(int id)
        {
            var model = _repository.Get(id);
            model.manager_role_values = _role_value.GetList("where role_id=@role_id", new { role_id = id }).ToList();
            return model;
        }

        /// <summary>
        /// 返回角色名称
        /// </summary>
        public string GetTitle(int id)
        {
            return _repository.Get(id).role_name;
        }

        public bool Update(Manager_role model)
        {
            return _repository.UpdateAll(model);
        }
    }
}